package init

import (
	"github.com/google/uuid"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"zg4/online-retailers/common/global"
	"zg4/online-retailers/common/inits"
	"zg4/online-retailers/common/proto/products"
)

func init() {
	inits.InitZap()
	inits.GetViper()
	InitConsul()
	InitProductGrpc()

}

func InitConsul() {
	consulConfig := global.Config.Consul
	client, err := inits.NewConsulClient(consulConfig.Host, consulConfig.Port)
	if err != nil {
		return
	}
	id := uuid.NewString()
	err = client.RegisterConsul(inits.Register{
		Id:      id,
		Name:    consulConfig.Name,
		Tags:    []string{"xx"},
		Port:    1112,
		Address: "localhost",
	})
	if err != nil {
		panic(err)
	}
	log.Println("consul register success")
}

func InitProductGrpc() {
	productConn, err := grpc.NewClient("localhost:8899", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}

	global.ProductClient = products.NewProductsSrvClient(productConn)

}
